package cn.ht.jywrwater.module.ht.service.waterqualitydata;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualitydata.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualitydata.WaterQualityDataDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterqualitydata.WaterQualityDataConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualitydata.WaterQualityDataMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水质数据 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterQualityDataServiceImpl implements WaterQualityDataService {

    @Resource
    private WaterQualityDataMapper waterQualityDataMapper;

    @Override
    public Long createWaterQualityData(WaterQualityDataCreateReqVO createReqVO) {
        // 插入
        WaterQualityDataDO waterQualityData = WaterQualityDataConvert.INSTANCE.convert(createReqVO);
        waterQualityDataMapper.insert(waterQualityData);
        // 返回
        return waterQualityData.getId();
    }

    @Override
    public void updateWaterQualityData(WaterQualityDataUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterQualityDataExists(updateReqVO.getId());
        // 更新
        WaterQualityDataDO updateObj = WaterQualityDataConvert.INSTANCE.convert(updateReqVO);
        waterQualityDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterQualityData(Long id) {
        // 校验存在
        validateWaterQualityDataExists(id);
        // 删除
        waterQualityDataMapper.deleteById(id);
    }

    private void validateWaterQualityDataExists(Long id) {
        if (waterQualityDataMapper.selectById(id) == null) {
            throw exception(WATER_QUALITY_DATA_NOT_EXISTS);
        }
    }

    @Override
    public WaterQualityDataDO getWaterQualityData(Long id) {
        return waterQualityDataMapper.selectById(id);
    }

    @Override
    public List<WaterQualityDataDO> getWaterQualityDataList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterQualityDataMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterQualityDataDO> getWaterQualityDataPage(WaterQualityDataPageReqVO pageReqVO) {
        return waterQualityDataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterQualityDataDO> getWaterQualityDataList(WaterQualityDataExportReqVO exportReqVO) {
        return waterQualityDataMapper.selectList(exportReqVO);
    }

}
